﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using StackExchange.Redis;


namespace CoreCms.Net.Caching.AutoMate.RedisCache
{
    public class RedisOperationRepository : IRedisOperationRepository
    {
        private readonly ILogger<RedisOperationRepository> _logger;
        private readonly ConnectionMultiplexer _redis;
        private readonly IDatabase _database;

        public RedisOperationRepository(ILogger<RedisOperationRepository> logger, ConnectionMultiplexer redis)
        {
            _logger = logger;
            _redis = redis;
            _database = redis.GetDatabase();
        }

        private IServer GetServer()
        {
            var endpoint = _redis.GetEndPoints();
            return _redis.GetServer(endpoint.First());
        }

        public async Task Clear()
        {
            foreach (var endPoint in _redis.GetEndPoints())
            {
                var server = GetServer();
                foreach (var key in server.Keys())
                {
                    await _database.KeyDeleteAsync(key);
                }
            }
        }


        /// <summary>
        /// 获取锁
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="cacheTime"></param>
        /// <returns></returns>
        public bool LockTake(string key, string value, TimeSpan cacheTime)
        {
            return _database.LockTake(key, value, cacheTime);

        }

        /// <summary>
        /// 异步获取锁
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="cacheTime"></param>
        /// <returns></returns>
        public async Task<bool> LockTakeAsync(string key, string value, TimeSpan cacheTime)
        {
            return await _database.LockTakeAsync(key, value, cacheTime);
        }

        /// <summary>
        /// 延长锁(续约)
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="cacheTime"></param>
        /// <returns></returns>

        public bool LockExtend(string key, string value, TimeSpan cacheTime)
        {
            return _database.LockExtend(key, value, cacheTime);
        }

        /// <summary>
        /// 延长锁(续约)
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="cacheTime"></param>
        /// <returns></returns>

        public async Task<bool> LockExtendAsync(string key, string value, TimeSpan cacheTime)
        {
            return await _database.LockExtendAsync(key, value, cacheTime);
        }

        /// <summary>
        /// 释放锁
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public bool LockRelease(string key, string value)
        {
            return _database.LockRelease(key, value);
        }

        /// <summary>
        /// 异步释放锁
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public async Task<bool> LockReleaseAsync(string key, string value)
        {
            return await _database.LockReleaseAsync(key, value);
        }

        /// <summary>
        /// 判断key是否存在
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public async Task<bool> KeyExistsAsync(string key)
        {

            return await _database.KeyExistsAsync(key);
        }

        /// <summary>
        /// 判断key是否存在
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public bool KeyExists(string key)
        {

            return _database.KeyExists(key);
        }

        /// <summary>
        /// 获取键过期时间
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public async Task<TimeSpan?> KeyTime(string key)
        {

            return await _database.KeyTimeToLiveAsync(key);
        }

        /// <summary>
        /// 获取值
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public async Task<string> Get(string key)
        {
            return await _database.StringGetAsync(key);
        }

        /// <summary>
        /// 获取值
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public RedisValue StringGet(string key)
        {
            return _database.StringGet(key);
        }

        /// <summary>
        /// 搜索匹配Key
        /// </summary>
        /// <param name="pattern"></param>
        /// <returns></returns>
        public List<string> SearchKey(string pattern)
        {

            var list = new List<string>();
            var script = "return redis.call('keys',@pattern)";
            var prepared = LuaScript.Prepare(script);
            var redisResult = _database.ScriptEvaluate(prepared, new { pattern });
            if (!redisResult.IsNull)
            {
                foreach (var key in (RedisKey[])redisResult)
                {

                    list.Add(key);
                }
            }
            return list;

        }

        public async Task KeyDeleteAsync(string key)
        {
            await _database.KeyDeleteAsync(key);
        }

        public void KeyDelete(string key)
        {
            _database.KeyDelete(key);
        }

        /// <summary>
        /// 模糊删除
        /// </summary>
        /// <param name="pattern"></param>
        public void LikeRemove(string pattern)
        {
            var redisResult = _database.ScriptEvaluate(LuaScript.Prepare(
                //Redis的keys模糊查询：
                " local res = redis.call('KEYS', @keypattern) " +
                " return res "), new { @keypattern = pattern });

            if (!redisResult.IsNull)
            {
                var preSult = (RedisKey[])redisResult;
                _database.KeyDelete(preSult);
            }
        }

        /// <summary>
        /// 模糊删除
        /// </summary>
        /// <param name="pattern"></param>
        public async Task LikeRemoveAsync(string pattern)
        {
            var redisResult = await _database.ScriptEvaluateAsync(LuaScript.Prepare(
                //Redis的keys模糊查询：
                " local res = redis.call('KEYS', @keypattern) " +
                " return res "), new { @keypattern = pattern });
            if (!redisResult.IsNull)
            {
                var preSult = (RedisKey[])redisResult;
                await _database.KeyDeleteAsync(preSult);
            }
        }

        /// <summary>
        /// 获取值
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="cacheTime"></param>
        /// <returns></returns>
        public async Task SetAsync(string key, object value, TimeSpan cacheTime)
        {
            if (value != null)
            {
                //序列化，将object值生成RedisValue
                await _database.StringSetAsync(key, JsonConvert.SerializeObject(value), cacheTime);
            }
        }

        /// <summary>
        /// 获取值
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="cacheTime"></param>
        /// <returns></returns>
        public async Task<bool> StringSetAsync(string key, string value, TimeSpan cacheTime)
        {

            return await _database.StringSetAsync(key, value, cacheTime);

        }

        /// <summary>
        /// 获取值
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="cacheTime"></param>
        /// <returns></returns>
        public bool StringSet(string key, string value, TimeSpan cacheTime)
        {
            return _database.StringSet(key, value, cacheTime);
        }

        /// <summary>
        /// 获取值
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public bool StringSet(string key, string value)
        {
            return _database.StringSet(key, value);
        }

        /// <summary>
        /// 获取值
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="cacheTime"></param>
        /// <returns></returns>
        public bool StringSet(string key, RedisValue value, TimeSpan cacheTime)
        {
            return _database.StringSet(key, value, cacheTime);
        }

        /// <summary>
        /// 获取值
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public bool StringSet(string key, RedisValue value)
        {
            return _database.StringSet(key, value);
        }

        /// <summary>
        /// Increment 递增
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public async Task<long> StringIncrement(string key, long value = 1)
        {
            return await _database.StringIncrementAsync(key, value);
        }

        /// <summary>
        /// 获取一个值并序列化
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <param name="key"></param>
        /// <returns></returns>
        public async Task<TEntity> Get<TEntity>(string key)
        {
            var value = await _database.StringGetAsync(key);
            //需要用的反序列化，将Redis存储的Byte[]，进行反序列化
            return value.HasValue ? JsonConvert.DeserializeObject<TEntity>(value) : default;
        }

        /// <summary>
        /// 根据key获取RedisValue
        /// </summary>
        /// <param name="redisKey"></param>
        /// <returns></returns>
        public async Task<RedisValue[]> ListRangeAsync(string redisKey)
        {
            return await _database.ListRangeAsync(redisKey);
        }

        /// <summary>
        /// 在列表头部插入值。如果键不存在，先创建再插入值
        /// </summary>
        /// <param name="redisKey"></param>
        /// <param name="redisValue"></param>
        /// <returns></returns>
        public async Task<long> ListLeftPushAsync(string redisKey, string redisValue)
        {
            return await _database.ListLeftPushAsync(redisKey, redisValue);
        }
        /// <summary>
        /// 在列表尾部插入值。如果键不存在，先创建再插入值
        /// </summary>
        /// <param name="redisKey"></param>
        /// <param name="redisValue"></param>
        /// <returns></returns>
        public async Task<long> ListRightPushAsync(string redisKey, string redisValue)
        {
            return await _database.ListRightPushAsync(redisKey, redisValue);
        }

        /// <summary>
        /// 在列表尾部插入数组集合。如果键不存在，先创建再插入值
        /// </summary>
        /// <param name="redisKey"></param>
        /// <param name="redisValue"></param>
        /// <returns></returns>
        public async Task<long> ListRightPushAsync(string redisKey, IEnumerable<string> redisValue)
        {
            var redislist = new List<RedisValue>();
            foreach (var item in redisValue)
            {
                redislist.Add(item);
            }
            return await _database.ListRightPushAsync(redisKey, redislist.ToArray());
        }


        /// <summary>
        /// 移除并返回存储在该键列表的第一个元素  反序列化
        /// </summary>
        /// <param name="redisKey"></param>
        /// <returns></returns>
        public async Task<T> ListLeftPopAsync<T>(string redisKey) where T : class
        {
            var cacheValue = await _database.ListLeftPopAsync(redisKey);
            if (string.IsNullOrEmpty(cacheValue)) return null;
            var res = JsonConvert.DeserializeObject<T>(cacheValue);
            return res;
        }

        /// <summary>
        /// 移除并返回存储在该键列表的最后一个元素   反序列化
        /// 只能是对象集合
        /// </summary>
        /// <param name="redisKey"></param>
        /// <returns></returns>
        public async Task<T> ListRightPopAsync<T>(string redisKey) where T : class
        {
            var cacheValue = await _database.ListRightPopAsync(redisKey);
            if (string.IsNullOrEmpty(cacheValue)) return null;
            var res = JsonConvert.DeserializeObject<T>(cacheValue);
            return res;
        }

        /// <summary>
        /// 移除并返回存储在该键列表的第一个元素   
        /// </summary>
        /// <param name="redisKey"></param>
        /// <returns></returns>
        public async Task<string> ListLeftPopAsync(string redisKey)
        {
            return await _database.ListLeftPopAsync(redisKey);
        }

        /// <summary>
        /// 移除并返回存储在该键列表的最后一个元素   
        /// </summary>
        /// <param name="redisKey"></param>
        /// <returns></returns>
        public async Task<string> ListRightPopAsync(string redisKey)
        {
            return await _database.ListRightPopAsync(redisKey);
        }

        /// <summary>
        /// 列表长度
        /// </summary>
        /// <param name="redisKey"></param>
        /// <returns></returns>
        public async Task<long> ListLengthAsync(string redisKey)
        {
            return await _database.ListLengthAsync(redisKey);
        }

        /// <summary>
        /// 返回在该列表上键所对应的元素
        /// </summary>
        /// <param name="redisKey"></param>
        /// <returns></returns>
        public async Task<IEnumerable<string>> ListRangeAsync(string redisKey, int db = -1)
        {
            var result = await _database.ListRangeAsync(redisKey);
            return result.Select(o => o.ToString());
        }

        /// <summary>
        /// 根据索引获取指定位置数据
        /// </summary>
        /// <param name="redisKey"></param>
        /// <param name="start"></param>
        /// <param name="stop"></param>
        /// <returns></returns>
        public async Task<IEnumerable<string>> ListRangeAsync(string redisKey, int start, int stop)
        {
            var result = await _database.ListRangeAsync(redisKey, start, stop);
            return result.Select(o => o.ToString());
        }

        /// <summary>
        /// 删除List中的元素 并返回删除的个数
        /// </summary>
        /// <param name="redisKey">key</param>
        /// <param name="redisValue">元素</param>
        /// <param name="type">大于零 : 从表头开始向表尾搜索，小于零 : 从表尾开始向表头搜索，等于零：移除表中所有与 VALUE 相等的值</param>
        /// <returns></returns>
        public async Task<long> ListDelRangeAsync(string redisKey, string redisValue, long type = 0)
        {
            return await _database.ListRemoveAsync(redisKey, redisValue, type);
        }

        /// <summary>
        /// 清空List
        /// </summary>
        /// <param name="redisKey"></param>
        public async Task ListClearAsync(string redisKey)
        {
            await _database.ListTrimAsync(redisKey, 1, 0);
        }


        /// <summary>
        /// 有序集合/定时任务延迟队列用的多
        /// </summary>
        /// <param name="redisKey">key</param>
        /// <param name="redisValue">元素</param>
        /// <param name="score">分数</param>
        public async Task<bool> SortedSetAddAsync(string redisKey, string redisValue, double score)
        {
            return await _database.SortedSetAddAsync(redisKey, redisValue, score);
        }

        /// <summary>
        /// 添加到有序集合 用时间
        /// </summary>
        /// <param name="key"></param>
        /// <param name="msg"></param>
        /// <param name="time"></param>
        /// <returns></returns>

        public async Task<bool> SortedSetAddAsync(string key, string msg, DateTime time)
        {
            var score = (time.ToUniversalTime().Ticks - 621355968000000000) / 10000000;
            var bl = await _database.SortedSetAddAsync(key, msg, score);
            //When.NotExists 不更新 直接添加
            return bl;
        }



        #region 小北 添加的 几个有序集合功能

        /// <summary>
        /// 获取有序集合索引
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public async Task<long?> SortedSetRankAsync(string key, string value)
        {
            var result = await _database.SortedSetRankAsync(key, value);

            return result;
        }

        /// <summary>
        /// 返回有序集中，成员的分数值。 如果成员元素不是有序集 key 的成员，或 key 不存在，返回 null
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public async Task<double?> SortedSetScoreAsync(string key, string value)
        {
            var result = await _database.SortedSetScoreAsync(key, value);

            return result;
        }


        public double? SortedSetScore(string key, string value)
        {
            var result = _database.SortedSetScore(key, value);

            return result;
        }


        /// <summary>
        /// 返回有序列表里的数据
        /// </summary>
        /// <param name="redisKey">RedisKey</param>
        /// <param name="start">0 是第一个</param>
        /// <param name="stop">最大分数值</param>
        /// <returns></returns>
        public async Task<IEnumerable<string>> SortedSetRangeByRankAsync(string redisKey, int start, int stop)
        {
            var result = await _database.SortedSetRangeByRankAsync(redisKey, start, stop);



            return result.Select(o => o.ToString());

        }



        /// <summary>
        /// 移出序列表里的指定范围数量
        /// </summary>
        /// <param name="redisKey">RedisKey</param>
        /// <param name="start">0 是第一个</param>
        /// <param name="stop">最大分数值</param>
        /// <returns></returns>
        public async Task<long> SortedSetRemoveRangeByRankAsync(string redisKey, int start, int stop)
        {
            return await _database.SortedSetRemoveRangeByRankAsync(redisKey, start, stop);
        }

        /// <summary>
        /// 返回有序列表里的指定范围数量
        /// </summary>
        /// <param name="redisKey">RedisKey</param>
        /// <param name="start">0 是第一个</param>
        /// <param name="stop">最大分数值</param>
        /// <returns></returns>
        public async Task<long> SortedSetLengthAsync(string redisKey, long min, long max)
        {
            return await _database.SortedSetLengthAsync(redisKey, min, max);

        }

        #endregion 小北 添加的 几个有序集合功能
    }
}
